package ezXpns;
/**
 * 
 * @author A0086934R
 * 
 * Tag is used to categorize an Expense or Income item. 
 */

//@author A0086934R
public class Tag {		
	/**
	 * Constructor that sets the name of the Tag
	 * @param name name of the Tag
	 */
	public Tag(String name) {
		this.name = name;
	}
	/**
	 * Constructor that sets the name and value of the Tag
	 * @param name name of the Tag
	 * @param value value of the Tag
	 */
	public Tag(String name, String value) {
		this(name);
		this.value = value;
	}
	/**
	 * Gets the name of the Tag
	 * @return name
	 */
	public String getName() { return name; }
	/**
	 * Checks if the Tag has a value
	 * @return True if Tag has a value. False if Tag does not have a value
	 */
	public boolean hasValue() { return value != null; }
	/**
	 * Gets the value as a String
	 * @return value as a String
	 */
	public String getValueAsString() { return value; }
	/**
	 * Gets the value as a Float
	 * @return value as a Float
	 */
	public Float getValueAsNumber() {
		try {
			if(value == null) {
				return null;
			}
			return Float.parseFloat(value);
		}
		catch(NumberFormatException e) {
			return null;
		}
	}
	/**
	 * Converts the Tag into a String name:value
	 */
	@Override
	public String toString() {
		if(value!=null) {
			return name + ":" + value.toString();
		}
		else
			return name;
	}
	/**
	 * compares two Tags. returns True if they are equal, false if they 
	 * are not equal
	 */
	@Override
	public boolean equals(Object other) {
		if(other == this) return true;
		if(other == null) return false;
		if(!(other instanceof Tag)) return false;
		
		Tag otherTag = (Tag) other;
		
		return otherTag.name.equals(name)
			&& (!hasValue() || value.equals(otherTag.value));//if has value, they must be equal
	}
	
	private String name;
	private String value;
}
